package com.zeekr.myapplication3

import android.os.SystemClock
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext

class MyTest2 {
    suspend fun myCall(): Flow<String> = callbackFlow {
        abc(1000, object : MyCall {
            override fun onCall(text: String) {
                super.onCall(text)
                trySendBlocking("fgfgfgfgg")
                trySendBlocking("654654654")
                trySendBlocking("ppppppp")
                trySendBlocking("okmjuiko")
                //trySend("")
            }
        })
        abc(5000, object : MyCall {
            override fun onCall(text: String) {
                super.onCall(text)
                trySendBlocking("000000000")
                trySendBlocking("888888888")
            }
        })

        awaitClose {}
    }

    suspend fun myCall2(): Flow<String> = channelFlow {
        abc(1000, object : MyCall {
            override fun onCall(text: String) {
                super.onCall(text)
                trySendBlocking("111")
                trySendBlocking("222")
                trySendBlocking("333")
                trySendBlocking("444")
                //trySend("")
            }
        })
        abc(5000, object : MyCall {
            override fun onCall(text: String) {
                super.onCall(text)
                trySendBlocking("555")
                trySendBlocking("666")
            }
        })

        awaitClose {}
    }

    private fun abc(ms: Long, call: MyCall) {
        Thread {
            SystemClock.sleep(ms)
            call.onCall("")
        }.start()
    }
}